<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <!-- 表单 -->
    <el-form
      label-width="100px"
      class="demo-ruleForm"
      :model="pwdForm"
      :rules="pwdRules"
      ref="formRef"
    >
      <el-form-item label="原密码" prop="old_pwd">
        <el-input
          type="password"
          autocomplete="off"
          v-model="pwdForm.old_pwd"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input
          type="password"
          autocomplete="off"
          v-model="pwdForm.new_pwd"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input
          type="password"
          autocomplete="off"
          v-model="pwdForm.re_pwd"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="doClick">提交</el-button>
        <el-button @click="$refs.formRef.resetFields()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: "UserPwd",
  data() {
    return {
      //表单数据
      pwdForm: {
        old_pwd: "",
        new_pwd: "",
        re_pwd: "",
      },
      //表单校验
      pwdRules: {
        // 旧密码规则
        old_pwd: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            pattern: /^\S{6,15}$/,
            message: "密码必须是6-15位的非空字符",
            trigger: "blur",
          },
        ],
        // 新密码规则
        new_pwd: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            pattern: /^\S{6,15}$/,
            message: "密码必须是6-15位的非空字符",
            trigger: "blur",
          },
        ],
        // 确认密码规则
        re_pwd: [
          { required: true, message: "请再次确认密码", trigger: "blur" },
          {
            pattern: /^\S{6,15}$/,
            message: "密码必须是6-15位的非空字符",
            trigger: "blur",
          },
          // 使用 validator 属性，来应用自定义的校验规则
          {
            validator: (rule, value, callback) => {
              // 形参中的 value 表示被绑定的元素的值
              if (value !== this.pwdForm.new_pwd) {
                // 校验失败
                callback(new Error("两次密码不一致！"));
              } else {
                // 校验成功
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    doClick() {
      //(1)检查表单是否校验成功
      this.$refs.formRef.validate(async (valid) => {
        // valid 就是表单验证的结果，如果是true，表示通过了
        console.log(valid);
        //(2)如果成功,则发送注册ajax
        if (valid) {
          //解构赋值 const {属性名:属性值}=对象名
          const { data: res } = await this.$axios.patch(
            "/my/updatepwd",
            this.pwdForm
          );
          console.log(res);
          if (res.code === 0) {
            this.$message.success(res.message);
          } else {
            this.$message.warning(res.message);
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.el-form {
  width: 500px;
}
</style>
